package com.stox.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.stox.model.BseDataStream;
import com.stox.model.entity.BarEntity;
import com.stox.model.entity.ExchangeEntity;

@Component
public class BseDataStreamParser implements DataStreamParser {

	@Autowired
	private BseBarParser bseBarParser;
	
	@Autowired
	private BseScripParser bseScripParser;
	
	@Override
	public BseDataStream parse(ExchangeEntity exchange, String rawData) {
		List<BarEntity> barList = new ArrayList<BarEntity>();
		if(StringUtils.hasText(rawData)){
			StringTokenizer t = new StringTokenizer(rawData,"\n",false);
			while(t.hasMoreTokens()){
				String token = t.nextToken();
				BarEntity bar = bseBarParser.parse(token);
				if(null != bar){
					bar.setScrip(bseScripParser.parse(exchange, token));
					barList.add(bar);
				}
			}
		}
		return new BseDataStream(barList);
	}
	
}
